Skip to content

fix(security): strengthen prompt-injection nd backtick defenses - #23221

Closed
likhith-adithya wants to merge 9 commits into
google-gemini:mainfrom
likhith-adithya:fix-security-injections-v2
Closed

likhith-adithya wants to merge 9 commits into
google-gemini:mainfrom
likhith-adithya:fix-security-injections-v2

Conversation

@likhith-adithya

Copy link
Copy Markdown

Summary

This PR resolves the prompt-injection vulnerabilities outlined in #23114, specifically patching the Command Injection via Backtick Substitution flaw.

It improves upon a previously closed attempt (#23170) by implementing the necessary security fixes caught during review:

Backtick Fix: Removed the regex length limit (/[^]+/`) to catch all legacy backtick substitutions regardless of payload size.
Multi-line Substitution: Added the dotAll (s) flag to the $(...) regex to prevent bypasses utilizing line breaks.
Fence-Jumping Defense: Escaped static fence markers in user content to prevent attackers from breaking out of the isolation block.
FilePath Sanitization: Sanitized the filePath parameter to prevent attackers from injecting into the warning header via malicious file naming.

Fixes #23114

This replaces PR #23213 with a clean commit history to pass the Google CLA check.

@likhith-adithya
likhith-adithya requested review from a team as code owners March 20, 2026 08:08
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the security posture of the system by implementing robust defenses against prompt injection attacks. It introduces a new utility for detecting various malicious patterns and integrates this detection into the file reading process. The changes ensure that potentially harmful content is identified, wrapped with clear security warnings, and isolated, thereby preventing instruction overrides, tool call injections, and command substitutions.

Highlights

  • Prompt Injection Vulnerability Resolution: This PR addresses and resolves prompt-injection vulnerabilities, specifically focusing on Command Injection via Backtick Substitution.
  • Enhanced Backtick Defense: The regex for detecting backtick substitutions was updated by removing the length limit (/[^]+/) to ensure all legacy backtick substitutions are caught, regardless of payload size.
  • Multi-line Substitution Prevention: The dotAll (s) flag was added to the $(...) regex to prevent bypasses that utilize line breaks in shell command substitutions.
  • Fence-Jumping Defense: Static fence markers within user content are now escaped to prevent attackers from breaking out of isolation blocks.
  • File Path Sanitization: The filePath parameter is sanitized to prevent injection into warning headers through malicious file naming.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new prompt injection detection utility (prompt-injection-detector.ts) that scans LLM-generated content for suspicious patterns like instruction overrides, system prompt markers, and apparent tool calls. This utility is integrated into the ReadFile tool to wrap suspicious content with a security warning. Review comments highlight two critical vulnerabilities in the newly added regex patterns: the backtick substitution pattern ([^]+) should remove its length limit to prevent command injection via large payloads, and the $(...)shell command substitution pattern requires thes` (dotAll) flag to prevent multi-line bypasses.

Note: Security Review did not run due to the size of the PR.

@gemini-cli gemini-cli Bot added the area/security Issues related to security label Mar 20, 2026
@likhith-adithya

Copy link
Copy Markdown
Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces significant security enhancements against prompt injection attacks by adding detection heuristics and content wrapping for files read by the read_file tool. The changes are well-structured, with the core logic encapsulated in a new prompt-injection-detector.ts utility. The defenses for fence-jumping and header injection are good additions. I've found one potential bypass in the regex for backtick command detection that should be addressed.

Note: Security Review did not run due to the size of the PR.

@gemini-cli

gemini-cli Bot commented Apr 4, 2026

Copy link
Copy Markdown
Contributor

Hi there! Thank you for your interest in contributing to Gemini CLI.

To ensure we maintain high code quality and focus on our prioritized roadmap, we have updated our contribution policy (see Discussion #17383).

We only guarantee review and consideration of pull requests for issues that are explicitly labeled as 'help wanted'. All other community pull requests are subject to closure after 14 days if they do not align with our current focus areas. For this reason, we strongly recommend that contributors only submit pull requests against issues explicitly labeled as 'help-wanted'.

This pull request is being closed as it has been open for 14 days without a 'help wanted' designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding and for being part of our community!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/security Issues related to security size/m A medium sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Strengthen prompt-injection defenses when reading project content

2 participants